﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
#if DOT_NET
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
#endif
using StyleMVVM.UnitTests.TestClasses.SimpleObjects;
using StyleMVVM.DependencyInjection;
using StyleMVVM.UnitTests.TestClasses.SimpleObjects;

namespace StyleMVVM.UnitTests.DependencyInjection
{
	[TestClass]
	public class ExportInstanceTests
	{
		[TestMethod]
		public void RegisterInstance()
		{
			Bootstrapper bootstrapper = new Bootstrapper();

			bootstrapper.Container.Register<SimpleType>().As<SimpleType>().
			             InitializeProperty(x => x.TestInt, 5);

			bootstrapper.Container.Register<SimpleExport>().As<SimpleExport>().
			             ImportConstructor(() => new SimpleExport()).
			             InitializeProperty(x => x.TestString, "Hello").
			             ImportProperty(x => x.TestType).
			             ActivationMethod(x => x.Activate());

			bootstrapper.Start();

			SimpleExport export = bootstrapper.Container.Locate<SimpleExport>();

			Assert.IsNotNull(export, "export null");
			Assert.AreEqual("Hello", export.TestString, "test string not equal");
			Assert.IsNotNull(export.TestType, "export testtype null");
			Assert.AreEqual(5, export.TestType.TestInt, "TestInt are not equal");

			bootstrapper.Shutdown();
		}

		[TestMethod]
		public void RegisterInterfaces()
		{
			Bootstrapper bootstrapper = new Bootstrapper();

			bootstrapper.Container.Register<SimpleType>().
			             As<SimpleType>().
			             InitializeProperty(x => x.TestInt, 5);

			bootstrapper.Container.Register<SimpleExport>().
			             As<SimpleExport>().
			             ImportConstructor(() => new SimpleExport()).
			             InitializeProperty(x => x.TestString, "Hello").
			             ImportProperty(x => x.TestType).
			             ActivationMethod(x => x.Activate());

			bootstrapper.Container.RegisterAssembly(GetType().GetTypeInfo().Assembly).
			             ExportInterface<ITestInterface>();

			bootstrapper.Start();

			ITestInterface testInterface = bootstrapper.Container.Locate<ITestInterface>();

			SimpleExport export = testInterface.SimpleImport;

			Assert.IsNotNull(export, "export null");
			Assert.AreEqual("Hello", export.TestString, "test string not equal");
			Assert.IsNotNull(export.TestType, "export test type null");
			Assert.AreEqual(5, export.TestType.TestInt, "TestInt are not equal");

			bootstrapper.Shutdown();
		}

		[TestMethod]
		public void TestBaseExport()
		{
			Bootstrapper bootstrapper = new Bootstrapper();

			bootstrapper.Container.RegisterAssembly(GetType().GetTypeInfo().Assembly).
			             ExportTypesBasedOn(typeof(BaseExportType));

			bootstrapper.Start();

			InheritingType newValue = (InheritingType)bootstrapper.Container.Locate("InheritingType");

			Assert.IsNotNull(newValue);

			bootstrapper.Shutdown();
		}

		[TestMethod]
		public void RegisterExportFunction()
		{
			Bootstrapper bootstrapper = new Bootstrapper();

			bootstrapper.Container.RegisterExportFunction((x, y, z) => new SimpleType()).As<SimpleType>();

			bootstrapper.Start();

			Assert.IsNotNull(bootstrapper.Container.Locate<SimpleType>());

			bootstrapper.Shutdown();
		}
	}
}